<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>发送邮件</title>
    <link rel="stylesheet" href="js/layui/css/layui.css" media="all">
    <style type="text/css">
        .newTraining_sample .layui-form-label {
            width: 115px;
        }

        .layui-form-item .layui-input-inline {
            width: 360px;
        }

        #showEmailsDiv .layui-badge {
            margin: 7px;
        }
    </style>
</head>

<body>
<div style="padding: 40px; background-color: #F2F2F2;">
    <div class="layui-card">
        <form class="layui-form" method="post">
            <div class="layui-card-header" style="font-weight:bold;font-size: 18px;padding: 0px 100px">发送邮件</div>
            <div class="layui-card-body newStudent">
                <div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">收件人</label>
                        <div id="showEmailsDiv" class="layui-input-inline">

                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">主题</label>
                        <div class="layui-input-inline">
                            <input type="text" id="subject" name="subject" required lay-verify="required|name"
                                   placeholder="请填写邮件主题" autocomplete="on" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">正文</label>
                        <div class="layui-input-inline">
                            <textarea class="layui-input" name="text" lay-verify="required" style="height: 300px"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">附件</label>
                        <div class="layui-upload">
                            <button type="button" class="layui-btn" id="choosePhoto" style="vertical-align: top">选择</button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input type="button" class="layui-btn" lay-submit lay-filter="sendEmail" value="发送邮件" />
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="js/layui/layui.js" charset="utf-8"></script>
<script>
    var $;
    layui.use(['form', 'upload', 'element', 'layer'], function () {
        const form = layui.form;
        const upload = layui.upload;
        const layer = layui.layer;
        $ = layui.jquery;

        // 渲染表单元素
        form.render();
        // 点击按钮提交到servlet
        const index = parent.layer.getFrameIndex(window.name);//获取窗口索引

        // 回显邮箱信息
        var emailsStr = location.search.substr(1).split("=")[1];
        var emails = JSON.parse(atob(emailsStr));
        var emailSpans = [];
        for (var i = 0; i < emails.length; i++) {
            emailSpans.push('<span class="layui-badge">' + emails[i] + '</span>')
        }
        showEmailsDiv.innerHTML = emailSpans.join("");

        // 渲染文件上传
        //常规使用 - 普通图片上传
        const uploadInst = upload.render({
            elem: '#choosePhoto'
            , url: 'xxx' //改成您自己的上传接口
            , field: 'attachment'
            , auto: false   // 不手动进行提交
            // , bindAction: '#btn_servlet' // 绑定按钮触发上传
            , size: 1024 * 2 // 限制附件不超过2M大小
            , before: function (obj) {
                layer.load('上传中', {icon: 16, time: 0});
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                $('#upload_result').html(''); //置空上传失败的状态

                layer.closeAll('loading'); //关闭loading
                parent.layer.close(index);
                parent.layui.table.reload("table_student");
            }
            , error: function () {
                layer.closeAll('loading'); //关闭loading
            }
        });

        // 监听提交
        // 提交到servlet
        // 点击按钮提交到servlet
        form.on('submit(sendEmail)', function (data) {
            submitFormAndUploadAttachment('uploadAttachmentAndSendEmail', data);

            return false;
        });

        function submitFormAndUploadAttachment(url, data) {
            data.field.tos = emails.join(",");
            uploadInst.reload({
                url: url,
                data: data.field
            });
            uploadInst.upload();
        }
    });
</script>
</body>

</html>